package com.sanploy.fed.push.util;

import com.google.gson.Gson;
import org.springframework.web.socket.WebSocketSession;

import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by Administrator on 2016/8/14.
 */
public class FileTest {

//    public static void main(String[] args) {
//        try {
//            new FileCreat().throwMethod();
//        }catch (Exception e){
//            System.out.println("xxxx");
//        }
//    }
//    public static void main(String[] args) {
//        Gson gson = new Gson();
//        List<XX> list = new ArrayList<XX>();
//        for (int i = 0;i<10;i++){
//            XX xx = new XX();
//            xx.setType(String.valueOf(i));
//            xx.setValue("s");
//            list.add(xx);
//        }
//        System.out.println(gson.toJson(list));
//        ConcurrentHashMap<String, String> terminnals = new ConcurrentHashMap<String, String>();
//        terminnals.put("sss1", "ssqs");
//        terminnals.put("sss2", "ssscs");
//        terminnals.put("sss3", "sss1");
//        terminnals.put("sss3", "sss2");
//        terminnals.put("sss3", "sss3");
//        Collection<String> keys = terminnals.values();
//        Iterator<String> s = keys.iterator();
//        while (s.hasNext()) {
//            System.out.println(s.next());
//        }

//    }

    public static String getJarPath() {
        File file = getFile();
        if (file == null)
            return null;
        return file.getAbsolutePath();
    }

    /**
     * 获取jar目录
     *
     * @return
     */
    public static String getJarDir() {
        File file = getFile();
        if (file == null)
            return null;
        return getFile().getParent();
    }

    /**
     * 获取jar包名
     *
     * @return
     */
    public static String getJarName() {
        File file = getFile();
        if (file == null)
            return null;
        return getFile().getName();
    }

    /**
     * 获取当前Jar文件
     *
     * @return
     */
    private static File getFile() {
        // 关键是这行...
        String path = FileTest.class.getProtectionDomain().getCodeSource().getLocation().getFile();
        try {
            path = java.net.URLDecoder.decode(path, "UTF-8"); // 转换处理中文及空格
        } catch (java.io.UnsupportedEncodingException e) {
            return null;
        }
        return new File(path);
    }
    private static class XX{
        private String type;

        private String value;

        public String getValue() {
            return value;
        }

        public void setValue(String value) {
            this.value = value;
        }

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }
    }
}


